package com.ship.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ship.common.core.base.BaseController;
import com.ship.common.core.base.Page;
import com.ship.contract.bean.*;
import com.ship.contract.bean.dto.ConResourceMemorandumDto;
import com.ship.contract.bean.dto.SubmitPortNamesDto;
import com.ship.contract.bean.vo.PortNameVo;
import com.ship.contract.bean.vo.QueryTimeZoneSwitchingInfo;
import com.ship.contract.service.ConResourceFullNameService;
import com.ship.contract.service.ExportService;
import com.ship.common.core.utils.HttpRequestUtil;
import com.ship.common.core.utils.SpringUtil;
import com.ship.common.core.validate.Add;
import com.ship.common.core.validate.Update;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.contract.bean.vo.ConResourceMemorandumVo;
import com.ship.contract.service.ConResourceMemorandumPositionService;
import com.ship.contract.service.ConResourceMemorandumService;
import com.ship.contract.task.ExportContractDataTask;
import com.ship.contract.utils.ModuleConst;
import com.ship.system.api.RemoteUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 31463
 */
@RestController
@RequestMapping("/conResourceMemorandum")
public class ConResourceMemorandumController extends BaseController {

    @Autowired
    private ConResourceMemorandumService conResourceMemorandumService;

    @Autowired
    private ConResourceFullNameService conResourceFullNameService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private HttpRequestUtil httpRequestUtil;

    @Autowired
    private SpringUtil springUtil;

    @Resource
    private RemoteUserService remoteUserService;

    @Autowired
    private ConResourceMemorandumPositionService conResourceMemorandumPositionService;

    /**
     * 分页
     *
     * @param conResourceMemorandum
     * @return
     */
    @PostMapping("/page")
    public JsonResult<Page<ConResourceMemorandum>> page(@RequestBody ConResourceMemorandum conResourceMemorandum) {
        //conResourceMemorandum.setDelApply();
        Page<ConResourceMemorandum> page = conResourceMemorandumService.page(conResourceMemorandum,new QueryWrapper<ConResourceMemorandum>().ne("del_apply",2).orderByDesc("delivery_window_begin"));
        return JsonResult.success(page);
    }

    /**
     * 查询资源合同
     *
     * @return
     */
    @PostMapping("/list")
    public JsonResult<List<ConResourceMemorandumVo>> selectAllContractNumbers(@RequestBody ConResourceMemorandumSelect dto) {
        List<ConResourceMemorandumVo> models = conResourceMemorandumService.selectAllContractNumbers(dto);
        return JsonResult.success(models);
    }

    /**
     * 新增
     *
     * @param conResourceMemorandum
     * @return
     */
    @PostMapping
    @Log(title = "新增资源合同(备忘录)",businessType = BusinessType.INSERT)
    public JsonResult<Object> add(@RequestBody @Validated(Add.class)ConResourceMemorandum conResourceMemorandum) {
        conResourceMemorandumService.saveCon(conResourceMemorandum);
        return JsonResult.success();
    }

    /**
     * 修改
     *
     * @param conResourceMemorandum
     * @return
     */
    @PutMapping
    @Log(title = "修改资源合同(备忘录)",businessType = BusinessType.UPDATE)
    public JsonResult<Object> update(@RequestBody @Validated(Update.class)ConResourceMemorandum conResourceMemorandum) {
        conResourceMemorandumService.updateCon(conResourceMemorandum);
        return JsonResult.success();
    }

    /**
     * 删除申请
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Log(title = "删除资源合同(备忘录)",businessType = BusinessType.DELETE)
    public JsonResult delete(@PathVariable Long id){
        conResourceMemorandumService.delApply(id);
        return JsonResult.success();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping
    public JsonResult<Map<String,Object>> detail(Long id) {
        return JsonResult.success(conResourceMemorandumService.detail(id));
    }

    @ApiOperation("根据合同ID格式化时间")
    @GetMapping("/query/timeZoneById")
    public JsonResult queryTimezoneById(@RequestParam Long id,@RequestParam(required = false) String type) throws ParseException {
        QueryTimeZoneSwitchingInfo info = conResourceMemorandumService.queryTimezoneById(id,type);
        return JsonResult.success(info);
    }


    /**
     * 申请优秀案例
     * @param conExcellentCases
     * @return
     */
    @PostMapping("/excellent")
    public JsonResult excellent(@RequestBody ConExcellentCases conExcellentCases){
        conResourceMemorandumService.excellent(conExcellentCases, SecurityUtils.getUsername());
        return JsonResult.success();
    }

    /**
     * 导出
     *
     * @param conResourceMemorandum
     * @return
     */
    @PostMapping("/export")
    @Log(title = "导出资源合同(备忘录)",businessType = BusinessType.EXPORT)
    public JsonResult<Object> export(@RequestBody ConResourceMemorandum conResourceMemorandum) {
        ExportContractDataTask task = new ExportContractDataTask();
        task.setBaseBean(conResourceMemorandum);
        task.setBaseService(conResourceMemorandumService);
        task.setMethodsName("page");
        task.setModule(ModuleConst.CON_RESOURCE_MEMORANDUM);
        task.setDownloadFileName("导出资源合同-备忘录.zip");
        task.setHttpRequestUtil(httpRequestUtil);
        task.setSpringUtil(springUtil);
        task.setUserId(SecurityUtils.getUserId());
        exportService.export(task);
        return JsonResult.success();
    }

    @GetMapping("/fieldLocation")
    public JsonResult<Object> fieldLocation(String id){
        char ch = '"';
        Long ids = Long.valueOf(id.replace(String.valueOf(ch),""));
        List<ConResourceMemorandumPosition> result = conResourceMemorandumPositionService.getFieldLocation(ids);
        Map<String, Map<String, Object>> resultMap = new HashMap<>();
        for (ConResourceMemorandumPosition position : result) {
            String fieldName = position.getFiledName();
            Map<String, Object> fieldData = new HashMap<>();
            fieldData.put("name",position.getOldName());
            fieldData.put("x1", position.getX1());
            fieldData.put("x2", position.getX2());
            fieldData.put("y1", position.getY1());
            fieldData.put("y2", position.getY2());
            fieldData.put("page", position.getPage());
            fieldData.put("filedValue", position.getFiledValue());
            resultMap.put(fieldName + "Loc", fieldData);
        }
        return JsonResult.success(resultMap);
    }

    @GetMapping(value = {"/getPortNames","/getPortNames/{name}"})
    public JsonResult getPortNames(@PathVariable(required = false) String name){
        List<PortNameVo> portNameVos = conResourceMemorandumService.getPortNames(name);
        return JsonResult.success(portNameVos);
    }

    @PostMapping("/submitPortNames")
    public JsonResult submitPortNames(@RequestBody SubmitPortNamesDto submitPortNamesDto){
        List<PortNameVo> portNameVos = conResourceMemorandumService.getPortNames(submitPortNamesDto.getName());
        return JsonResult.success(portNameVos);
    }

    @PostMapping ("/getConResourceMemorandums")
    public JsonResult getConResourceMemorandums(@RequestBody ConResourceMemorandumDto conResourceMemorandumDto) {
        ConResourceMemorandum conResourceMemorandum = new ConResourceMemorandum();
        conResourceMemorandum.setMainContract(conResourceMemorandumDto.getMainContractId());
        Page<ConResourceMemorandum> page = conResourceMemorandumService.page(conResourceMemorandum,new QueryWrapper<ConResourceMemorandum>().eq("main_contract",conResourceMemorandumDto.getMainContractId()).ne("del_apply",2));
        return JsonResult.success(page);
    }

    @GetMapping("/getResourceFullName")
    public JsonResult<List<ConResourceFullName>> ConResourceFullName() {
        return JsonResult.success(conResourceFullNameService.getConIdByUser(SecurityUtils.getUsername(),null));
    }
}
